#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTreeWidget>
#include <QApplication>
#include <QPainter>
#include <QPixmap>
#include <QTabWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //connect(ui->treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
    //            this, SLOT(setLabelBackground(QTreeWidgetItem*,int)));
    //room_hide = new QPropertyAnimation(ui->label, "geometry");
    path = new QString(QApplication::applicationDirPath());
    ui->statusBar->showMessage(QString("SystemBox: Queimado"));

    ui->treeWidget->expandAll();
    ui->treeWidget_2->expandAll();
    ui->treeWidget_3->expandAll();

    ui->actionModulos->setChecked(true);
    ui->menuBar->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7e7e7, stop: 1 #cfcfcf);");
    ui->mainToolBar->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #828282, stop: 1 #525252); border-width: 1px; border: black;");

    ui->frame_toolbar_eventos->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #828282, stop: 1 #525252); border-width: 1px; border: black;");
    //connect(room_hide, SIGNAL(finished()), this, SLOT(setLabelFrame()));

    ui->dockWidget->setMinimumHeight(0);
    ui->dockWidget->setMaximumHeight(50);

    ui->dockWidgetContents->setMinimumHeight(0);
    ui->dockWidgetContents->setMaximumHeight(50);

    connect(ui->dockWidget, SIGNAL(dockLocationChanged(Qt::DockWidgetArea *area)), this, SLOT(resizeDockDispositivos(Qt::DockWidgetArea *area)));

    view = new MainView();
    view->show();
    view->setParent(ui->frame_label_central);
    view->setGeometry(ui->frame_label_central->rect());

    //QLayout *layout = new QVBoxLayout();
    //layout->addWidget(view);
    //ui->frame_central->setLayout(layout);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setLabelBackground(QTreeWidgetItem *item, int column)
{
    ui->frame_label_central->setAutoFillBackground(false);
    QString teste = item->text(column);

    if(item->text(column).contains("Floor 1", Qt::CaseInsensitive))
        ui->frame_label_central->setStyleSheet("background-image: url(/floor2.png);");
        animRoom(false);

    if(item->text(column).contains("Floor 2", Qt::CaseInsensitive))
        ui->frame_label_central->setStyleSheet("background-image: url(/floor2.png);");

    if(item->parent()){

    if(item->parent()->text(0).contains("Floor", Qt::CaseInsensitive)){
        /*ui->frame_label_central->setStyleSheet("background-image: url(/floor2.png);");
        switch(ui->treeWidget->currentIndex().row()){
        case 0:
            ui->label->setVisible(true);
            ui->label->setPixmap(QPixmap(QString("%1/suite.png").arg(*path)));
            ui->label->setFrameShape(QFrame::WinPanel);
            animRoom(true);
            break;
        case 1:
            ui->label->setVisible(true);
            ui->label->setPixmap(QPixmap(QString("%1/room2.png").arg(*path)));
            ui->label->setFrameShape(QFrame::WinPanel);
            animRoom(true);
            break;
        case 2:
            ui->label->setVisible(true);
            ui->label->setPixmap(QPixmap(QString("%1/room3.png").arg(*path)));
            ui->label->setFrameShape(QFrame::WinPanel);
            animRoom(true);
            break;
        case 3:
            ui->label->setVisible(true);
            ui->label->setPixmap(QPixmap(QString("%1/3D/dorm3.png").arg(*path)));
            ui->label->setFrameShape(QFrame::WinPanel);
            animRoom(true);
            break;
            }*/
        }
    }
    ui->statusBar->showMessage(QString("SystemBox: Queimado | ") + item->text(0));
}

void MainWindow::animRoom(bool show)
{/*
    if(show){
        anim = new QPropertyAnimation(ui->label, "geometry");
        anim->setEasingCurve(QEasingCurve::Linear);
        anim->setDuration(500);
        anim->setStartValue(QRectF(ui->label->geometry().center().x(), ui->label->geometry().center().y(), 1,1));
        anim->setEndValue(QRectF(30,30, ui->frame_label_central->width()-60, ui->frame_label_central->height()-60));
        anim->start();
    }
    else{
        //room_hide = new QPropertyAnimation(ui->label, "geometry");
        room_hide->setEasingCurve(QEasingCurve::Linear);
        room_hide->setDuration(500);
        room_hide->setStartValue(QRectF(30,30, ui->frame_label_central->width()-60, ui->frame_label_central->height()-60));
        room_hide->setEndValue(QRectF(ui->label->geometry().center().x(), ui->label->geometry().center().y(), 1,1));
        room_hide->start();
    }
    */
}

void MainWindow::on_actionModulos_triggered()
{/*
    if(ui->actionModulos->isChecked()){
        ui->frame_direito->setDisabled(false);
        anim2 = new QPropertyAnimation(ui->frame_label_central, "geometry");
        anim2->setEasingCurve(QEasingCurve::Linear);
        anim2->setDuration(500);
        anim2->setStartValue(QRectF(ui->frame_label_central->pos().x(), ui->frame_label_central->pos().y(),
                                   ui->frame_label_central->width()+ui->frame_direito->width(), ui->frame_label_central->height()));
        anim2->setEndValue(QRectF(ui->frame_label_central->pos().x(), ui->frame_label_central->pos().y(),
                                  ui->frame_label_central->width()-ui->frame_direito->width(), ui->frame_label_central->height()));
        anim2->start();

        anim3 = new QPropertyAnimation(ui->frame_direito, "pos");
        anim3->setEasingCurve(QEasingCurve::Linear);
        anim3->setDuration(500);
        anim3->setStartValue(QPointF(ui->centralWidget->width(), 0));
        anim3->setEndValue(QPointF(ui->frame_direito->pos().x()-(ui->frame_direito->width()), 0));
        anim3->start();
    }
    else
    {
        anim2 = new QPropertyAnimation(ui->frame_label_central, "geometry");
        anim2->setEasingCurve(QEasingCurve::Linear);
        anim2->setDuration(500);
        anim2->setStartValue(QRectF(ui->frame_label_central->pos().x(), ui->frame_label_central->pos().y(),
                                    ui->frame_label_central->geometry().width(), ui->frame_label_central->height()));
        anim2->setEndValue(QRectF(ui->frame_label_central->pos().x(), ui->frame_label_central->pos().y(),
                                  ui->frame_label_central->width()+ui->frame_direito->width(), ui->frame_label_central->height()));
        anim2->start();

        anim3 = new QPropertyAnimation(ui->frame_direito, "pos");
        anim3->setEasingCurve(QEasingCurve::Linear);
        anim3->setDuration(500);
        anim3->setStartValue(QPointF(ui->frame_direito->pos().x(), 0));
        anim3->setEndValue(QPointF(ui->splitter->width(), 0));
        anim3->start();
        ui->frame_direito->setDisabled(true);
    }*/
}

void MainWindow::setLabelFrame()
{
    //ui->label->setFrameShape(QFrame::NoFrame);
    //ui->label->setVisible(false);
}

void MainWindow::on_actionExit_triggered()
{
    this->close();
}

void MainWindow::resizeDockDispositivos(Qt::DockWidgetArea *area)
{
    /*if(area == Qt::TopDockWidgetArea){
        ui->dockWidget->setMinimumHeight(0);
        ui->dockWidget->setMaximumHeight(50);

        ui->dockWidgetContents->setMinimumHeight(0);
        ui->dockWidgetContents->setMaximumHeight(50);
    }

    if(area == Qt::LeftDockWidgetArea){
        ui->dockWidget->setMinimumSize(0,0);
        ui->dockWidget->setMaximumSize(70,800);

        ui->dockWidgetContents->setMinimumSize(0,0);
        ui->dockWidgetContents->setMaximumSize(70,800);
    }*/
}

void MainWindow::on_actionDispositivos_triggered()
{
    if(ui->dockWidget->isVisible())
        ui->dockWidget->setVisible(false);
    else ui->dockWidget->setVisible(true);
}

void MainWindow::on_actionSkin_1_triggered()
{
    view->setGeometry(ui->frame_label_central->rect());
}

void MainWindow::on_splitter_4_splitterMoved(int pos, int index)
{
    view->setGeometry(ui->frame_label_central->rect());
}
